﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using System.Globalization;

namespace JCard
{
    public partial class fEditVoc : Form
    {
        #region Variables for resource
        ResourceManager objResourceManager;
        CultureInfo objCulInfo;
        #endregion

        #region Variables
        DTO_VOC dtoVoc;
        public DTO_VOC UpdateDtoVoc
        {
            get { return dtoVoc; }
        }
        string dbPath;
        #endregion

        #region constructors
        public fEditVoc(DTO_VOC dtoVoc, string dbPath)
        {
            this.dtoVoc = dtoVoc;
            this.dbPath = dbPath;

            InitializeComponent();

            SetDisplayLabel();
        }

        private void SetDisplayLabel()
        {
            objResourceManager = new ResourceManager("JCard.Resources", typeof(fCLesson).Assembly);
            objCulInfo = new CultureInfo(Common.GetConfigValue(Constants.CONFIG_LANGUAGE_KEY, Constants.CONFIG_LANGUAGE_VALUE));
        }
        #endregion        

        #region Loading
        private void fEditVoc_Load(object sender, EventArgs e)
        {
            this.txtKanji.Text = dtoVoc.Kanji;
            this.txtHiragana.Text = dtoVoc.Hiragana;
            this.txtHanNom.Text = dtoVoc.Hannom;
            this.txtMeaning.Text = dtoVoc.Meaning;
        }
        #endregion        

        #region Thao tác trên form
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtHiragana.Text != string.Empty)
            {
                dtoVoc.Kanji = txtKanji.Text;
                dtoVoc.Hiragana = txtHiragana.Text;
                dtoVoc.Meaning = txtMeaning.Text;
                dtoVoc.Hannom = txtHanNom.Text;

                BUS_JCARD buVoc = new BUS_JCARD(dbPath);

                try
                {
                    buVoc.UpdateVocabulary(dtoVoc);
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                catch (Exception ex)
                {
                    Common.ShowErrorMsg(objCulInfo, objResourceManager, ex.Message);
                }
            }
            else
            {
                Common.ShowWarningMsg(objCulInfo, objResourceManager, Constants.RES_HIRAGANA_EMPTY_NAME, Constants.RES_HIRAGANA_EMPTY_VALUE);
            }
        }
        #endregion
    }
}
